package com.kcht.np.util;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;
import java.util.List;

/**
  * <p>
  * RESTful API 统一返回实体类
  * </p>
  *
  * @Package:        com.kcht.np.mp.util
  * @Description:    RESTful API 统一返回实体类
  * @Author:         liuqi
  * @CreateDate:     2020/3/31 22:50
 */

@Getter
@Setter
public class JsonResult<T> implements Serializable {

    private static final long serialVersionUID = 1456468469856161L;

    /**
     * 返回状态码
     */
    private Integer status;

    /**
     * 返回消息
     */
    private String message;

    /**
     * 返回内容
     */
    private T data;

    private JsonResult() {
    }

    /**
     * 自定义返回状态码
     * @param status
     * @param message
     * @param data
     * @param <T>
     * @return JsonResult
     */
    private static <T> JsonResult result(int status, String message, T data) {
        JsonResult result = new JsonResult();
        result.status = status;
        result.message = message;
        result.data = data;
        return result;
    }

    /**
     * @Description: 自定义返回状态码
     * @param:       [httpStatus, data]
     * @return:      JsonResult
     * @Author       liuqi
     * @CreateDate   2020/3/31 23:00
     */
    private static <T> JsonResult result(HttpStatus httpStatus, T data) {
        return result(httpStatus.status, httpStatus.value, data);
    }

    /**
     * 自定义返回状态码
     * @param status
     * @param message
     * @param <T>
     * @return
     */
    public static <T> JsonResult result(int status, String message) {
        return result(status, message, null);
    }

    /**
     * @Description: 操作成功
     * @param:
     * @return:      JsonResult
     * @Author       liuqi
     * @CreateDate   2020/3/31 23:11
     */
    public static <T> JsonResult success() {
        return success(HttpStatus.SUCCESS.value, null);
    }

    /**
     * 操作成功
     * @param totalPage
     * @param total
     * @param list
     * @param <T>
     * @return
     */
    public static <T> JsonResult success(long totalPage, long total, List<T> list) {
        return success(new ResultPage<T>(totalPage,list.size(), total, list));
    }


    /**
     * @Description: 操作成功
     * @param:       [resultPage]
     * @return:      JsonResult
     * @Author       liuqi
     * @CreateDate   2020/3/31 23:13
     */
    public static <T> JsonResult success(ResultPage<T> resultPage) {
        return success(HttpStatus.SUCCESS.value, resultPage);
    }

    /**
     * @Description:  操作成功
     * @param:       [message]
     * @return:      JsonResult
     * @Author       liuqi
     * @CreateDate   2020/3/31 23:15
     */
    public static <T> JsonResult success(String message) {
        return success(message, null);
    }

    /**
     * @Description: 操作成功
     * @param:       [data]
     * @return:      JsonResult
     * @Author       liuqi
     * @CreateDate   2020/3/31 23:16
     */
    public static <T> JsonResult success(T data) {
        return success(HttpStatus.SUCCESS.value, data);
    }

    /**
     * @Description: 操作成功
     * @param:       [message, data]
     * @return:      JsonResult
     * @Author       liuqi
     * @CreateDate   2020/3/31 23:18
     */
    public static <T> JsonResult success(String message, T data) {
        return result(HttpStatus.SUCCESS.status, message, data);
    }

    /**
     * @Description: 操作失败
     * @param:       [message]
     * @return:      JsonResult
     * @Author       liuqi
     * @CreateDate   2020/3/31 23:19
     */
    public static <T> JsonResult fail(String message) {
        return result(HttpStatus.FAIL.status, message, null);
    }

    /**
     * @Description: 未授权
     * @param:       []
     * @return:      JsonResult
     * @Author       liuqi
     * @CreateDate   2020/3/31 23:19
     */
    public static <T> JsonResult error401() {
        return result(HttpStatus.UNAUTHORIZED.status, HttpStatus.UNAUTHORIZED.value, null);
    }


    /**
     * @Description: 不支持的请求类型
     * @param:       []
     * @return:      JsonResult
     * @Author       liuqi
     * @CreateDate   2020/3/31 23:19
     */
    public static <T> JsonResult error415() {
        return result(HttpStatus.UNSUPPORTED_MEDIA_TYPE.status, HttpStatus.UNSUPPORTED_MEDIA_TYPE.value, null);
    }


    /**
     * @Description: 404 未找到
     * @param:       []
     * @return:      JsonResult
     * @Author       liuqi
     * @CreateDate   2020/3/31 23:20
     */
    public static <T> JsonResult error404() {
        return result(HttpStatus.NOT_FOUND.status, HttpStatus.NOT_FOUND.value, null);
    }


    /**
     * @Description: 服务器内部错误
     * @param:       []
     * @return:      JsonResult
     * @Author       liuqi
     * @CreateDate   2020/3/31 23:20
     */
    public static <T> JsonResult error500() {
        return result(HttpStatus.INTERNAL_SERVER_ERROR.status, HttpStatus.INTERNAL_SERVER_ERROR.value, null);
    }


    @Getter
    @Setter
    @AllArgsConstructor
    @NoArgsConstructor
    public static class ResultPage<T> {
        /**
         * 当前一共有多少页
         * */
        private long quantityOfPage;

        /**
         * 当前页实际大小
         */
        private long currentPageSize;


        /**
         * 总共，有多少条
         */
        private long total;

        /**
         * 数据
         */
        private List<T> list;

    }

    /**
     * 可以自行扩展
     */
    @Getter
    public enum HttpStatus {

        /**
         * @Description:   正常 返回代码
         * @CreateDate:    2020/3/31 23:21
         */
        SUCCESS(200, "操作成功"),

        /**
         * @Description:   错误 返回代码
         * @CreateDate:    2020/3/31 23:23
         */
        FAIL(100, "操作失败"),

        /**
         * @Description:   参数检验失败 返回代码
         * @CreateDate:    2020/3/31 23:24
         */
        VALIDATE_FAILED(304, "参数检验失败"),

        /**
         * @Description:   404 为找到
         * @CreateDate:    2020/3/31 23:24
         */
        NOT_FOUND(404, "Not Found"),

        /**
         * @Description:   未经授权
         * @CreateDate:    2020/3/31 23:24
         */
        UNAUTHORIZED(401, "Full authentication is required to access this resource"),

        /**
         * @Description:   不支持的媒体类型
         * @CreateDate:    2020/3/31 23:24
         */
        UNSUPPORTED_MEDIA_TYPE(415, "不支持此请求类型！"),


        /**
         * @Description:   服务器内部错误 返回代码
         * @CreateDate:    2020/3/31 23:24
         */
        INTERNAL_SERVER_ERROR(500, "服务器内部错误");

        private int status;

        private String value;

        HttpStatus(int status, String value) {
            this.status = status;
            this.value = value;
        }

    }

}
